import request from '@/utils/request'

// GitLab服务器管理
export const gitlabServerApi = {
  // 获取GitLab服务器列表
  getServers() {
    return request({
      url: '/api/gitlab/gitlab-servers/',
      method: 'get'
    })
  },

  // 创建GitLab服务器
  createServer(data: any) {
    return request({
      url: '/api/gitlab/gitlab-servers/',
      method: 'post',
      data
    })
  },

  // 更新GitLab服务器
  updateServer(id: number, data: any) {
    return request({
      url: `/api/gitlab/gitlab-servers/${id}/`,
      method: 'put',
      data
    })
  },

  // 删除GitLab服务器
  deleteServer(id: number) {
    return request({
      url: `/api/gitlab/gitlab-servers/${id}/`,
      method: 'delete'
    })
  },

  // 测试连接
  testConnection(id: number) {
    return request({
      url: `/api/gitlab/gitlab-servers/${id}/test_connection/`,
      method: 'post'
    })
  },

  // 测试所有连接
  testAllConnections() {
    return request({
      url: '/api/gitlab/gitlab-servers/test_all_connections/',
      method: 'post'
    })
  },

  // 同步项目
  syncProjects(id: number) {
    return request({
      url: `/api/gitlab/gitlab-servers/${id}/sync_projects/`,
      method: 'post'
    })
  },

  // 同步用户
  syncUsers(id: number) {
    return request({
      url: `/api/gitlab/gitlab-servers/${id}/sync_users/`,
      method: 'post'
    })
  },

  // 同步本地用户到GitLab
  syncLocalUsersToGitlab(id: number) {
    return request({
      url: `/api/gitlab/gitlab-servers/${id}/sync_local_users_to_gitlab/`,
      method: 'post'
    })
  },

  // 获取GitLab项目列表
  getProjects(id: number) {
    return request({
      url: `/api/gitlab/gitlab-servers/${id}/get_projects/`,
      method: 'get'
    })
  },

  // 配置Webhooks
  configureWebhooks(id: number) {
    return request({
      url: `/api/gitlab/gitlab-servers/${id}/configure_webhooks/`,
      method: 'post'
    })
  },

  // 同步历史数据
  syncHistoryData() {
    return request({
      url: '/api/gitlab/gitlab-servers/sync_history_data/',
      method: 'post'
    })
  },

  // 获取项目成员
  getProjectMembers(serverId: number, projectId: number) {
    return request({
      url: `/api/gitlab/gitlab-servers/${serverId}/get_project_members/`,
      method: 'get',
      params: { project_id: projectId }
    })
  },

  // 添加项目成员
  addProjectMember(serverId: number, projectId: number, userId: number, accessLevel: number) {
    return request({
      url: `/api/gitlab/gitlab-servers/${serverId}/add_project_member/`,
      method: 'post',
      data: {
        project_id: projectId,
        user_id: userId,
        access_level: accessLevel
      }
    })
  },

  // 更新项目成员权限
  updateProjectMember(serverId: number, projectId: number, userId: number, accessLevel: number) {
    return request({
      url: `/api/gitlab/gitlab-servers/${serverId}/update_project_member/`,
      method: 'post',
      data: {
        project_id: projectId,
        user_id: userId,
        access_level: accessLevel
      }
    })
  },

  // 移除项目成员
  removeProjectMember(serverId: number, projectId: number, userId: number) {
    return request({
      url: `/api/gitlab/gitlab-servers/${serverId}/remove_project_member/`,
      method: 'post',
      data: {
        project_id: projectId,
        user_id: userId
      }
    })
  },

  // 在GitLab上创建项目
  createGitlabProject(id: number, data: any) {
    return request({
      url: `/api/gitlab/gitlab-servers/${id}/create_gitlab_project/`,
      method: 'post',
      data
    })
  }
}

// 项目管理
export const projectApi = {
  // 获取项目列表
  getProjects() {
    return request({
      url: '/api/gitlab/projects/',
      method: 'get'
    })
  },

  // 创建项目
  createProject(data: any) {
    return request({
      url: '/api/gitlab/projects/',
      method: 'post',
      data
    })
  },

  // 更新项目
  updateProject(id: number, data: any) {
    return request({
      url: `/api/gitlab/projects/${id}/`,
      method: 'put',
      data
    })
  },

  // 删除项目
  deleteProject(id: number) {
    return request({
      url: `/api/gitlab/projects/${id}/`,
      method: 'delete'
    })
  }
}

// 环境管理
export const environmentApi = {
  // 获取环境列表
  getEnvironments() {
    return request({
      url: '/api/gitlab/environments/',
      method: 'get'
    })
  },

  // 创建环境
  createEnvironment(data: any) {
    return request({
      url: '/api/gitlab/environments/',
      method: 'post',
      data
    })
  },

  // 更新环境
  updateEnvironment(id: number, data: any) {
    return request({
      url: `/api/gitlab/environments/${id}/`,
      method: 'put',
      data
    })
  },

  // 删除环境
  deleteEnvironment(id: number) {
    return request({
      url: `/api/gitlab/environments/${id}/`,
      method: 'delete'
    })
  }
}

// GitLab项目管理
export const gitlabProjectApi = {
  // 获取GitLab项目列表
  getProjects(params?: any) {
    return request({
      url: '/api/gitlab/gitlab-projects/',
      method: 'get',
      params
    })
  },

  // 创建GitLab项目
  createProject(data: any) {
    return request({
      url: '/api/gitlab/gitlab-projects/',
      method: 'post',
      data
    })
  },

  // 更新GitLab项目
  updateProject(id: number, data: any) {
    return request({
      url: `/api/gitlab/gitlab-projects/${id}/`,
      method: 'patch',
      data
    })
  },

  // 删除GitLab项目
  deleteProject(id: number) {
    return request({
      url: `/api/gitlab/gitlab-projects/${id}/`,
      method: 'delete'
    })
  },

  // 从GitLab同步项目
  syncFromGitlab(data: any) {
    return request({
      url: '/api/gitlab/gitlab-projects/sync_from_gitlab/',
      method: 'post',
      data
    })
  },

  // 获取项目分支
  getBranches(projectId: number) {
    return request({
      url: `/api/gitlab/gitlab-projects/${projectId}/branches/`,
      method: 'get'
    })
  },

  // 获取项目提交记录
  getCommits(projectId: number, branch: string = 'master') {
    return request({
      url: `/api/gitlab/gitlab-projects/${projectId}/commits/`,
      method: 'get',
      params: { branch }
    })
  },

  // 获取项目详情
  getProject(projectId: number) {
    return request({
      url: `/api/gitlab/gitlab-projects/${projectId}/`,
      method: 'get'
    })
  }
}

// GitLab用户管理
export const gitlabUserApi = {
  // 获取GitLab用户列表
  getUsers(params?: any) {
    return request({
      url: '/api/gitlab/gitlab-users/',
      method: 'get',
      params
    })
  },

  // 从GitLab同步用户
  syncFromGitlab(data: any) {
    return request({
      url: '/api/gitlab/gitlab-users/sync_from_gitlab/',
      method: 'post',
      data
    })
  },

  // 映射到本地用户
  mapToLocalUser(id: number, data: any) {
    return request({
      url: `/api/gitlab/gitlab-users/${id}/map_to_local_user/`,
      method: 'post',
      data
    })
  },

  // 取消用户映射
  unmapUser(id: number) {
    return request({
      url: `/api/gitlab/gitlab-users/${id}/unmap_user/`,
      method: 'post'
    })
  }
}

// 用户项目权限管理
export const userProjectPermissionApi = {
  // 获取用户项目权限列表
  getPermissions(params?: any) {
    return request({
      url: '/api/gitlab/user-project-permissions/',
      method: 'get',
      params
    })
  },

  // 创建用户项目权限
  createPermission(data: any) {
    return request({
      url: '/api/gitlab/user-project-permissions/',
      method: 'post',
      data
    })
  },

  // 更新用户项目权限
  updatePermission(id: number, data: any) {
    return request({
      url: `/api/gitlab/user-project-permissions/${id}/`,
      method: 'put',
      data
    })
  },

  // 删除用户项目权限
  deletePermission(id: number) {
    return request({
      url: `/api/gitlab/user-project-permissions/${id}/`,
      method: 'delete'
    })
  }
}

// 本地用户管理
export const userApi = {
  // 获取用户列表
  getUsers(params?: any) {
    return request({
      url: '/api/users/',
      method: 'get',
      params
    })
  }
}

// GitLab审计日志
export const gitlabAuditApi = {
  // 获取GitLab审计日志
  getAudits(params?: any) {
    return request({
      url: '/api/gitlab/gitlab-audit/',
      method: 'get',
      params
    })
  }
}

// DevOps时间线
export const devopsTimelineApi = {
  // 获取DevOps时间线
  getTimeline(params?: any) {
    return request({
      url: '/api/gitlab/devops-timeline/',
      method: 'get',
      params
    })
  }
}
